﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Windows.Threading;
using AutoMapper;
using Microsoft.Practices.Prism;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;
using Sautom.ClientComunication;
using Sautom.ClientComunication.QueriesService;
using Sautom.ClientComunication.Services;
using Sautom.I18N;
using Sautom.Modules.Proposal.Models;

namespace Sautom.Modules.Proposal.ViewModels
{
    public sealed class ProposalIndexViewModel : NavigationAwareNotificationObject, IRegionMemberLifetime
    {
        #region Constructor
        public ProposalIndexViewModel(IRegionManager regionManager, ServiceFactory serviceFactory)
        {
            RegionManager = regionManager;

            EditProposalCommand = new DelegateCommand<string>(ExecuteEditProposalCommand, (isCreateNew) => isCreateNew == "true" || SelectedItem != null);
            ListCountiresCommand = new DelegateCommand(() => RegionManager.RequestNavigate(RegionProvider.MainRegion, PathProvider.CountriesList));
            RatesCommand = new DelegateCommand(() => RegionManager.RequestNavigate(RegionProvider.MainRegion, PathProvider.RateManagement));

            QueriesServiceClient = serviceFactory.GetQueriesService();
        }
        #endregion

        #region Tech properties

        public IQueriesService QueriesServiceClient { get; set; }
        #endregion

        #region Commands
        public DelegateCommand<string> EditProposalCommand { get; private set; }
        public DelegateCommand ListCountiresCommand { get; private set; }
        public DelegateCommand RatesCommand { get; set; }

        private void ExecuteEditProposalCommand(string isCreateNew)
        {
            var uriQuery = new UriQuery();
            if (isCreateNew != "true" && SelectedItem != null)
            {
                uriQuery.Add("proposalId", SelectedItem.Id.ToString());
            }

            var uri = new Uri(PathProvider.ProposalEdit + uriQuery, UriKind.Relative);

            RegionManager.RequestNavigate(RegionProvider.MainRegion, uri);
        }

        #endregion

        #region For View properties

        private IRegionManager RegionManager { get; set; }

        private bool _isIndividualFilter;
        public bool IsIndividualFilter
        {
            get { return _isIndividualFilter; }
            set
            {
                _isIndividualFilter = value;
                UpdateModelFromService();
            }
        }

        private bool _isGroupFilter;
        public bool IsGroupFilter
        {
            get { return _isGroupFilter; }
            set
            {
                _isGroupFilter = value;
                UpdateModelFromService();
            }
        }

        private string _countryFilter;
        public string CountryFilter
        {
            get { return _countryFilter; }
            set
            {
                _countryFilter = value;
                UpdateModelFromService();
            }
        }

        private string _schoolNameFilter;
        public string SchoolNameFilter
        {
            get { return _schoolNameFilter; }
            set
            {
                _schoolNameFilter = value;
                UpdateModelFromService();
            }
        }

        private string _courseNameFilter;
        public string CourseNameFilter
        {
            get { return _courseNameFilter; }
            set
            {
                _courseNameFilter = value;
                UpdateModelFromService();
            }
        }

        private ProposalModel _selectedItem;
        public ProposalModel SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                RaisePropertyChanged(() => SelectedItem);
                EditProposalCommand.RaiseCanExecuteChanged();
            }
        }

        private ObservableCollection<ProposalModel> _proposals;
        public ObservableCollection<ProposalModel> Proposals
        {
            get { return _proposals; }
            set
            {
                _proposals = value;
                RaisePropertyChanged(() => Proposals);
            }
        }

        #endregion

        #region Implementation of INavigationAware

        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            UpdateModelFromService();
        }

        #endregion

        #region Implementation of IRegionMemberLifetime

        public bool KeepAlive
        {
            get { return true; }
        }

        #endregion

        #region Private members
        private void UpdateModelFromService()
        {
            Mapper.CreateMap<ProposalDtoOutput, ProposalModel>()
                .ForMember(d => d.ProposalType, o => o.ResolveUsing(v => !v.IsGroupType ? ProposalResources.ProposalIndividualTypeLabel : ProposalResources.ProposalGroupTypeLabel));

            bool? isGroupType = null;
            if (IsGroupFilter != IsIndividualFilter) isGroupType = IsGroupFilter;
            
            var uiDispather = Dispatcher.CurrentDispatcher;
            (new Thread(() =>
            {
                List<ProposalDtoOutput> proposals = QueriesServiceClient.GetAllProposals(isGroupType, CountryFilter, SchoolNameFilter, CourseNameFilter).ToList();
                uiDispather.BeginInvoke(DispatcherPriority.Normal,
                                        (Action)(() => Proposals = Mapper.Map(proposals, Proposals)));
            })).Start();
        }
        #endregion
    }
}